<?php

namespace App\Jobs;

use App\Models\Cash;
use App\Models\MoneyLog;
use App\Models\Store;
use App\Services\ConfigService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class StoreDeposit implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $model;
    protected $type;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(array $array)
    {
        [$cash,$type] = $array;
        $this->model = $cash;
        $this->type = $type;
    }

    /**
     * Execute the job.
     *

     */
    public function handle()
    {
        $info = $this->model;
        //查询设置的费率
        $price_rate = (new ConfigService)->getFormatConfig('cash_rate');
        $money = abs($info->money);
        $payMoney = bcsub($money,bcmul($money,($price_rate/1000),3),3);
        $result = app('wechat_api')->transfer->toBankCard([
            'partner_trade_no' => $info->cash_no,
            'enc_bank_no' => $info->card_no, // 银行卡号
            'enc_true_name' => $info->name,   // 银行卡对应的用户真实姓名
            'bank_code' => $info->bank_code, // 银行编号
            'amount' => $payMoney*100,  // 单位：分
            'desc' => '付款到银行卡',
        ]);
        //如果提现成功，就插入一条成功的日志记录并将提现状态设置为提现成功，  否则把记录状态设置为手动提现
        $where['id'] = $info->id;
        if($result['return_code']=='SUCCESS'){ //SUCCESS/FAIL  此字段是通信标识，非付款标识，付款是否成功需要查看result_code来判断
            if ($result['result_code']=='SUCCESS'){
                //付款成功,提现状态改为成功，并添加一条提现记录,处理店铺的余额
                //开启事务
                DB::beginTransaction();
                try {
                    $update['cash_status'] = 1;
                    $this->editModel($where,$update);
                    //处理店铺冻结资金和插入提现日志
                    $store_id = $info->store_id;
                    $store_model         = new Store();
                    $store_model->store_frozen_money -= $money;

                    $ml_model            = new MoneyLog();
                    $store_model = $store_model->find($store_id);
                    //现有资金
                    $ml_model->money_before      = $store_model->store_money;
                    $ml_model->user_id   = $store_id;
                    $ml_model->money     = $money;
                    $ml_model->is_type   = 0;
                    $ml_model->is_seller = 1;
                    $ml_model->platform_money = bcmul($money,$price_rate,2); //平台收取的手续费
                    $ml_model->wechat_money = round($result['cmms_amt']/100,2); //微信收取的手续费

                    $ml_model->name      = '资金提现';
                    $ml_model->info      = $info;
                    $ml_model->info = '提现成功';
                    //更改后资金
                    $ml_model->money_after     = $store_model->store_money;
                    $ml_model->info      = $info;

                    $ml_model->save();
                    $store_model->save();

                    DB::commit();
                } catch (\Throwable $e) {
                    DB::rollBack();
                    $update['type'] = 2;
                    $update['error_info'] = '提现失败';
                    $this->editModel($where,$update);
                }

            }else{
                //提现失败，将提现改为手动提现
                $update['type'] = 2;
                $update['error_info'] = $result['err_code_des'];
                $this->editModel($where,$update);
            }

        }else{
            //通信失败，将提现改为手动提现
            $update['type'] = 2;
            $update['error_info'] = $result['return_msg'];
            $this->editModel($where,$update);

        }

    }

    /**
     * @Notes  处理提现表的数据
     * @param $where
     * @param $update
     * @author ypp
     * @date 2021/3/15
     * @time 14:06
     */
    public function editModel($where,$update)
    {
        Cash::query()->where($where)->update($update);
    }

}
